const curl = require('./curl');
const xlsx = require('./xlsx');
const moment = require('moment');

const get_world_data = function () {
  const url = "http://m.look.360.cn/events/feiyan";

  return new Promise(function (resolve, reject) {
    curl.download2(url, function (res) {
      if (res) {
        let res_json = JSON.parse(res);
        let world_data_list = res_json.country;
        let china_data_list = res_json.data;

        console.log('获取360数据成功！');

        let data_array = [world_data_list, china_data_list];
        resolve(data_array);
      } else {
        reject('获取360数据失败！')
      }
    })
  });
}

const get_china_trend_data = function () {
  const url = "https://m.look.360.cn/events/feiyanTrend";

  return new Promise(function (resolve, reject) {
    curl.download(url, function (res) {
      if (res) {
        let res_json = JSON.parse(res);
        let timeline = res_json.data.timeline;
        console.log('获取360趋势数据成功！');
        resolve(timeline);
      } else {
        reject('获取360趋势数据失败！')
      }
    })
  });
}

const get_data = async function (file) {
  let world_data = await get_world_data();
  let timeline = await get_china_trend_data();

  let other_data_list = world_data[0];
  let china_data_list = world_data[1];

  let other_data_array = [
    ['国家', '确诊', '疑似', '治愈', '死亡', '确诊变化', '修改时间']
  ];
  other_data_list.forEach(ele => {
    let modifyTime = moment(ele.modifyTime).format('YYYY-MM-DD HH:mm:ss');
    other_data_array.push([ele.provinceName, ele.diagnosed, ele.suspected, ele.cured, ele.died, ele.diffDiagnosed, modifyTime]);
  });

  let china_province_data_array = [
    ['省份编码', '省份名称', '确诊', '疑似', '治愈', '死亡', '确诊2', '疑似2', '治愈2', '死亡2', '创建时间', '修改时间']
  ];
  let china_city_data_array = [
    ['省份编码', '省份名称', '地市名称', '确诊', '疑似', '治愈', '死亡', '确诊变化', '创建时间', '修改时间']
  ];

  china_data_list.forEach(province => {
    let element = province.data;
    let createTime = moment(element.createTime).format('YYYY-MM-DD HH:mm:ss');
    let modifyTime = moment(element.modifyTime).format('YYYY-MM-DD HH:mm:ss');

    china_province_data_array.push([element.locationId, element.provinceName,
      element.confirmedCount, element.suspectedCount, element.curedCount, element.deadCount,
      province.diagnosed, province.suspected, province.cured, province.died, createTime, modifyTime
    ]);

    if (province.cities) {
      province.cities.forEach(ele => {
        china_city_data_array.push([element.locationId, element.provinceName,
          ele.cityName, ele.diagnosed, ele.suspected, ele.cured, ele.died, ele.diffDiagnosed, createTime, modifyTime
        ]);
      });
    }
  });

  let china_trend_array = [
    ['日期', '确诊', '疑似', '治愈', '死亡', '确诊变化', '疑似变化', '治愈变化', '死亡变化']
  ];

  timeline.forEach(ele => {
    china_trend_array.push([
      ele.time,
      ele.total.diagnosed, ele.total.suspected, ele.total.cured, ele.total.died,
      ele.diff.diagnosed, ele.diff.suspected, ele.diff.cured, ele.diff.died
    ])
  });

  let new_file = new xlsx.Xlsx(file);

  new_file.add({
    name: '全球',
    data: other_data_array
  });

  new_file.add({
    name: '全国每日趋势',
    data: china_trend_array
  });

  new_file.add({
    name: '省份',
    data: china_province_data_array
  });

  new_file.add({
    name: '地市',
    data: china_city_data_array
  });

  await new_file.writeFileSync();
}

exports.get_data = get_data;